package com.yoomiito.app.adapter.goods.list

import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.formatNum
import com.yoomiito.app.model.gift.GiftGoods
import com.yoomiito.app.model.home.FeaturedGoodsInfo
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.widget.CouponView
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout

import java.util.ArrayList

class NewGiftAdapter(data: List<GiftGoods>?) : BaseQuickAdapter<GiftGoods, BaseViewHolder>(R.layout.item_gife_goods_1, data) {

    override fun convert(helper: BaseViewHolder, item: GiftGoods) {
        val view = helper.getView<View>(R.id.item_gift_goods_v)
        val lp = view.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
        lp.leftMargin = DensityUtil.dp2px(12f)
        lp.rightMargin = DensityUtil.dp2px(12f)
        view.layoutParams = lp
        val awardView = helper.getView<CouponView>(R.id.awardView)
        val couponView = helper.getView<CouponView>(R.id.couponView)
        couponView.visibility = if (item.yo_money == 0) View.GONE else View.VISIBLE
        couponView.setText(item.yo_money.toString() + "优米")
        couponView.setHeaderText("送")
        //product_type 0-表示正常VIP产品，1-表示3650Vip产品,2-爆品',
        val goodsIv = helper.getView<ImageView>(R.id.item_gift_pic)
        val iv = helper.getView<ImageView>(R.id.goodsFlag)
        val titleTv = helper.getView<TextView>(R.id.goodsTitle)
        val tagFlowLayout = helper.getView<TagFlowLayout>(R.id.item_gift_flowLayout)
        val s = ArrayList<String>()
        if (!item.product_point.isNullOrEmpty()) {
            s.add(item.product_point)
        }
        tagFlowLayout.adapter = object : TagAdapter<String>(s) {
            override fun getView(parent: FlowLayout, position: Int, o: String): View {
                val tv = LayoutInflater.from(tagFlowLayout.context).inflate(R.layout.item_goods_flag, parent, false) as TextView
                tv.text = if (o.length > 8) o.substring(0, 8) + "..." else o
                return tv
            }
        }
        val priceTv = helper.getView<TextView>(R.id.item_gift_goods_price)
        //        TextView oldPriceTv = helper.getView(R.id.item_gift_goods_old_price);
        val saleSizeTv = helper.getView<TextView>(R.id.item_gift_goods_sale_size)

        ImageLoader.getInstance().into(goodsIv.context, item.banner, goodsIv, R.drawable.default_long_pic)


        //大礼包标记
        iv.visibility = if (0 == item.product_type) View.GONE else View.VISIBLE

        titleTv.text = item.product_name

        //奖标记
        if (App.mAppConfig.isLogin) {
            if (SpannableUtils.isEmptyOr0(item.commission)) {
                awardView.visibility = View.GONE
            } else {//vip以上显示奖
                awardView.visibility = if (UserInfoConfig.getRoleId() > 7) View.VISIBLE else View.GONE
                awardView.setText("¥" + SpannableUtils.handlePrice(item.commission))
            }
        } else {
            awardView.visibility = View.GONE
        }


        priceTv.text = SpannableUtils.getSmallMoneyFlag("¥" + SpannableUtils.handlePrice(item.sale_price))
        //        oldPriceTv.setText(SpannableUtils.isEmptyOr0(item.getMarket_price()) ? "专享价" : (SpannableUtils.getSpannable("¥" + SpannableUtils.handlePrice(item.getMarket_price()), 0)));
        saleSizeTv.text = "已售${item.volume.toString().formatNum()}"
    }
}
